#!/usr/bin/python
import sys
import psutil
import pymysql
import os

print(f"我们的操作系统是{sys.platform}")
#获取cpu的使用信息
used_cpu_percent = psutil.cpu_percent(interval=1)
idle_cpu_percent = 100 - used_cpu_percent

#获取内存的使用信息
used_mem_percent = psutil.virtual_memory().percent
free_mem_size= int(psutil.virtual_memory().free  / (1024**2))
total_mem_size = int(psutil.virtual_memory().total  / (1024**2))

#获取主机名
host_name = os.environ.get("HOSTNAME")

#连接到mysql 服务器里
conn = pymysql.connect(
    host='192.168.100.152',      # 数据库服务器地址
    user='sc',      # 数据库用户名
    password='sc123456',  # 数据库密码
    database='monitor',    # 数据库名
    port=3306,           # 端口号，MySQL默认是3306
)
#创建游标
cursor = conn.cursor()

insert_sql_cpu = "insert into cpuinfo(data_time,machine_name,used_percent,idle_percent) values(now(), %s, %s, %s)"
cursor.execute(insert_sql_cpu, (host_name,used_cpu_percent,idle_cpu_percent))
#插入内存信息
insert_sql_mem = "insert into meminfo(data_time,machine_name,used_mem_percent,free_mem,total_mem) values(now(), %s, %s, %s, %s)"
cursor.execute(insert_sql_mem, (host_name,used_mem_percent,free_mem_size,total_mem_size))
conn.commit()




